package com.lxz.aiagentbackend.tools;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import com.lxz.aiagentbackend.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: 小新
 * @Date: 2025/7/7 20:30
 * @Description: 文件操作工具类
 * @Version: v1.0
 */
@Configuration
public class FileOperationTool {
    private static final String ROOT_FILE_PATH = FileConstant.ROOT_FILE_PATH + "/file";

    // 读文件
    @Tool(description = "读取文件内容")
    public static String readFile(@ToolParam(description = "文件名称") String fileName) {
        // 通过hutool工具类读取文件
        String filePath = ROOT_FILE_PATH + "/" + fileName;
        try {
            return FileUtil.readUtf8String(filePath);
        } catch (Exception e) {
            return "读取文件失败："+ e.getMessage();
        }
    }


    // 写文件
    @Tool(description = "写入文件")
    public static String writeFile(@ToolParam(description = "文件名") String fileName,@ToolParam(description = "文件内容") String content) {
        try {
            String filePath = FileConstant.ROOT_FILE_PATH +"/"+ fileName;
            FileUtil.writeUtf8String(content, filePath);
            return "写入文件成功:"+ filePath;
        } catch (IORuntimeException e) {
            return "写入文件失败:"+ e.getMessage();
        }
    }
}
